﻿namespace FTIOT_CAT1
{
    public class 手持机配置
    {
        public List<Dictionary<string, object>> portConfig { get; set; }
        public List<Dictionary<string, object>> sensorList { get; set; }
        public 手持机配置()
        {
            portConfig = new();
            sensorList = new();
        }
    }
    /**
     设备地址	 名称	 寄存器地址	 寄存器长度	 数据类型	 上传名称	 LED显示	 地址寄存器	 类别	 权重
        1	   风速(机械)	 0	         1	        n.1	     windSpeed 	  风速:m    	2000	气象类     1
     **/
    //新建一个传感器类
    public class Sensor
    {
        public int DeviceAddress { get; set; } // 设备地址
        public string Name { get; set; } // 名称
        public int RegisterAddress { get; set; } // 寄存器地址
        public int RegisterLength { get; set; } // 寄存器长度
        public string DataType { get; set; } // 数据类型
        public string UploadName { get; set; } // 上传名称
        public string LEDDisplay { get; set; } // LED显示
        public int AddressRegister { get; set; } // 地址寄存器
        public string Category { get; set; } // 类别
        public int Weight { get; set; } // 权重+
        public float K { get; set; } // K
        public float B { get; set; } // B

        public Sensor(int deviceAddress, string name, int registerAddress, int registerLength, string dataType,
                      string uploadName, string ledDisplay, int addressRegister, string category, int weight, float k, float b)
        {
            DeviceAddress = deviceAddress;
            Name = name;
            RegisterAddress = registerAddress;
            RegisterLength = registerLength;
            DataType = dataType;
            UploadName = uploadName;
            LEDDisplay = ledDisplay;
            AddressRegister = addressRegister;
            Category = category;
            Weight = weight;
            K = k;
            B = b;
        }
        public Sensor(Sensor sensor) // 默认构造函数
        {
            this.DeviceAddress = sensor.DeviceAddress;
            this.Name = sensor.Name;
            this.RegisterAddress = sensor.RegisterAddress;
            this.RegisterLength = sensor.RegisterLength;
            this.DataType = sensor.DataType;
            this.UploadName = sensor.UploadName;
            this.LEDDisplay = sensor.LEDDisplay;
            this.AddressRegister = sensor.AddressRegister;
            this.Category = sensor.Category;
            this.Weight = sensor.Weight;
            this.K = sensor.K;
            this.B = sensor.B;
        }
        //判断两个传感器是否相等
        public override bool Equals(object? obj)
        {
            if (obj is Sensor otherSensor)
            {
                return DeviceAddress == otherSensor.DeviceAddress &&
                       Name == otherSensor.Name &&
                       RegisterAddress == otherSensor.RegisterAddress &&
                       RegisterLength == otherSensor.RegisterLength &&
                       DataType == otherSensor.DataType &&
                       UploadName == otherSensor.UploadName &&
                       LEDDisplay == otherSensor.LEDDisplay &&
                       AddressRegister == otherSensor.AddressRegister &&
                       Category == otherSensor.Category &&
                       K == otherSensor.K &&
                       B == otherSensor.B;

            }
            return false;
        }
        public bool Equalss(object? obj, bool notKB)
        {
            if (obj is Sensor otherSensor)
            {
                return DeviceAddress == otherSensor.DeviceAddress &&
                       Name == otherSensor.Name &&
                       RegisterAddress == otherSensor.RegisterAddress &&
                       RegisterLength == otherSensor.RegisterLength &&
                       DataType == otherSensor.DataType &&
                       UploadName == otherSensor.UploadName &&
                       LEDDisplay == otherSensor.LEDDisplay &&
                       AddressRegister == otherSensor.AddressRegister &&
                       Category == otherSensor.Category;
            }
            return false;
        }
        public override int GetHashCode() =>
           // Combine method supports up to 8 arguments. For 9 arguments, you can chain Combine calls.  
           HashCode.Combine(DeviceAddress, Name, RegisterAddress, RegisterLength, DataType, UploadName, LEDDisplay, AddressRegister) ^
           Category.GetHashCode();

    }
    // 新建一个传感器列表类
    public class SensorList
    {
        public List<Sensor> Sensors { get; set; } // 传感器列表
        public String type { get; set; } // 传感器类型
        public SensorList(string type)
        {
            Sensors = new List<Sensor>();
            this.type = type;
        }
        // 添加传感器到列表
        public bool AddSensor(Sensor sensor)
        {
            // 检查传感器是否已经存在
            if (Sensors.FindIndex(s => s.Equals(sensor)) != -1)
            {
                return false; // 如果传感器已存在，则不添加
            }
            Sensors.Add(sensor);
            return true; // 添加成功
        }
        //删除传感器
        public void RemoveSensor(Sensor? sensor)
        {
            // 检查传感器是否存在
            if (sensor == null) return; // 如果传感器为 null，直接返回
            if (!Sensors.Any(s => s.DeviceAddress == sensor.DeviceAddress && s.Name == sensor.Name))
            {
                return; // 如果传感器不存在，则不删除
            }
            Sensors.Remove(sensor);
        }
        public int GetSensorTypeCount()
        {
            return Sensors.Count;
        }
    }
    public class SensorTypeLis
    {
        public List<SensorList> Sensors { get; set; } // 传感器列表

        public SensorTypeLis()
        {
            Sensors = new List<SensorList>();
        }
        // 添加传感器到列表
        public void AddSensorType(SensorList sensor)
        {
            Sensors.Add(sensor);
        }
        // 获取传感器列表
        public SensorList GetSensorType(String type)
        {
            foreach (var sensorList in Sensors)
            {
                if (sensorList.type == type)
                {
                    return sensorList;
                }
            }
            return null; // 如果没有找到对应的传感器类型，返回 null
        }
    }
    public class DataGridViewList
    {
        public List<DataGridView> dataGridView { get; set; } // 传感器列表
        public DataGridViewList()
        {
            dataGridView = [];
        }
        // 添加传感器到列表
        public void AddSensorType(DataGridView dataGridView)
        {
            this.dataGridView.Add(dataGridView);
        }
        //获取列表长度
        public int GetSensorTypeCount()
        {
            return dataGridView.Count;
        }
        public DataGridView GetGridViewList(String name)
        {
            for (int i = 0; i < dataGridView.Count; i++)
            {
                if (dataGridView[i].Name == name)
                {
                    return dataGridView[i];
                }
            }
            return null;
        }
    }
    public static class ModbusUtils
    {
        // CRC16 计算
        public static byte[] CalculateCRC(byte[] data)
        {
            ushort crc = 0xFFFF;

            foreach (byte b in data)
            {
                crc ^= b;

                for (int i = 0; i < 8; i++)
                {
                    if ((crc & 0x0001) != 0)
                    {
                        crc >>= 1;
                        crc ^= 0xA001;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }

            return new byte[] { (byte)(crc & 0xFF), (byte)((crc >> 8) & 0xFF) }; // 返回低字节在前
        }
        public static byte[] CalculateCRC(byte[] data, int len)
        {
            ushort crc = 0xFFFF;
            for (int i = 0; i < len; i++)
            {
                crc ^= data[i];
                for (int j = 0; j < 8; j++)
                {
                    if ((crc & 0x0001) != 0)
                    {
                        crc >>= 1;
                        crc ^= 0xA001;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }
            return new byte[] { (byte)(crc & 0xFF), (byte)((crc >> 8) & 0xFF) }; // 返回低字节在前
        }
    }
}
